package com.frank.oj.controller.file;

import com.frank.oj.common.exception.StatusFailException;
import com.frank.oj.common.exception.StatusForbiddenException;
import com.frank.oj.service.file.ContestFileService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author frank
 * @Date 2024/4/8
 */
@Controller
@RequestMapping("/api/file")
@RequiresAuthentication
public class ContestFileController {
    @Resource
    private ContestFileService contestFileService;

    /**
     * 下载某个比赛的AC提交
     *
     * @param cid          比赛id
     * @param excludeAdmin 是否排除管理员
     * @param splitType    分割类型
     * @param response     响应
     * @throws StatusFailException      下载失败
     * @throws StatusForbiddenException 权限不足
     */
    @GetMapping("/download-contest-ac-submission")
    public void downloadContestACSubmission(@RequestParam("cid") Long cid,
                                            @RequestParam(value = "excludeAdmin", defaultValue = "false") Boolean excludeAdmin,
                                            @RequestParam(value = "splitType", defaultValue = "user") String splitType,
                                            HttpServletResponse response) throws StatusFailException, StatusForbiddenException {

        contestFileService.downloadContestACSubmission(cid, excludeAdmin, splitType, response);
    }

    /**
     * 下载某个比赛的打印文本
     */
    @GetMapping("/download-contest-print-text")
    @RequiresAuthentication
    public void downloadContestPrintText(@RequestParam("id") Long id, HttpServletResponse response) throws StatusForbiddenException {
        contestFileService.downloadContestPrintText(id, response);
    }

    /**
     * 下载比赛的排行榜（excel）
     */
    @GetMapping("/download-contest-rank")
    @RequiresAuthentication
    public void downloadContestRank(@RequestParam("cid") Long cid,
                                    @RequestParam("forceRefresh") Boolean forceRefresh,
                                    @RequestParam(value = "removeStar", defaultValue = "false") Boolean removeStar,
                                    @RequestParam(value = "containEnd", defaultValue = "false") Boolean isContainsAfterContestJudge,
                                    HttpServletResponse response) throws StatusFailException, IOException, StatusForbiddenException {
        contestFileService.downloadContestRank(cid, forceRefresh, removeStar, isContainsAfterContestJudge, response);
    }
}
